<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>加密函数 | siphan</title>
    <meta name="description" content="一个简单易用的 JavaScript 前端加密库">
    <meta name="generator" content="VuePress 1.3.0">
    
    
    <link rel="preload" href="/siphan/assets/css/0.styles.bab5f12a.css" as="style"><link rel="preload" href="/siphan/assets/js/app.a096c374.js" as="script"><link rel="preload" href="/siphan/assets/js/2.b96ecc85.js" as="script"><link rel="preload" href="/siphan/assets/js/5.537dfae2.js" as="script"><link rel="prefetch" href="/siphan/assets/js/10.e5803348.js"><link rel="prefetch" href="/siphan/assets/js/11.1506d706.js"><link rel="prefetch" href="/siphan/assets/js/12.9fea4da3.js"><link rel="prefetch" href="/siphan/assets/js/3.c42c2ebd.js"><link rel="prefetch" href="/siphan/assets/js/4.5f9aa696.js"><link rel="prefetch" href="/siphan/assets/js/6.c506cd42.js"><link rel="prefetch" href="/siphan/assets/js/7.0de11c07.js"><link rel="prefetch" href="/siphan/assets/js/8.83590e60.js"><link rel="prefetch" href="/siphan/assets/js/9.b0b386d5.js">
    <link rel="stylesheet" href="/siphan/assets/css/0.styles.bab5f12a.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container"><header class="navbar"><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/siphan/" class="home-link router-link-active"><img src="https://s2.ax1x.com/2020/02/26/3aERFe.png" alt="siphan" class="logo"> <span class="site-name can-hide">siphan</span></a> <div class="links"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/siphan/" class="nav-link">
  主页
</a></div><div class="nav-item"><a href="/siphan/guide/" class="nav-link">
  入门
</a></div><div class="nav-item"><a href="/siphan/API/" class="nav-link router-link-exact-active router-link-active">
  API
</a></div><div class="nav-item"><a href="http://service.shawroger.com/siphan" target="_blank" rel="noopener noreferrer" class="nav-link external">
  示例
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></div><div class="nav-item"><a href="https://gitee.com/shawroger/siphan" target="_blank" rel="noopener noreferrer" class="nav-link external">
  仓库
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></div> <!----></nav></div></header> <div class="sidebar-mask"></div> <aside class="sidebar"><nav class="nav-links"><div class="nav-item"><a href="/siphan/" class="nav-link">
  主页
</a></div><div class="nav-item"><a href="/siphan/guide/" class="nav-link">
  入门
</a></div><div class="nav-item"><a href="/siphan/API/" class="nav-link router-link-exact-active router-link-active">
  API
</a></div><div class="nav-item"><a href="http://service.shawroger.com/siphan" target="_blank" rel="noopener noreferrer" class="nav-link external">
  示例
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></div><div class="nav-item"><a href="https://gitee.com/shawroger/siphan" target="_blank" rel="noopener noreferrer" class="nav-link external">
  仓库
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></div> <!----></nav>  <ul class="sidebar-links"><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading open"><span>API</span> <span class="arrow down"></span></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/siphan/API/" class="active sidebar-link">加密函数</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/siphan/API/#checkkey" class="sidebar-link">checkKey</a></li><li class="sidebar-sub-header"><a href="/siphan/API/#createkey" class="sidebar-link">createKey</a></li><li class="sidebar-sub-header"><a href="/siphan/API/#encrypt" class="sidebar-link">encrypt</a></li><li class="sidebar-sub-header"><a href="/siphan/API/#encryptutf8" class="sidebar-link">encryptUTF8</a></li><li class="sidebar-sub-header"><a href="/siphan/API/#encryptas" class="sidebar-link">encryptAs</a></li><li class="sidebar-sub-header"><a href="/siphan/API/#encryptutf8as" class="sidebar-link">encryptUTF8As</a></li></ul></li><li><a href="/siphan/API/decrypt.html" class="sidebar-link">解密函数</a></li><li><a href="/siphan/API/config.html" class="sidebar-link">自定义对象</a></li></ul></section></li></ul> </aside> <main class="page"> <div class="theme-default-content content__default"><h1 id="加密函数"><a href="#加密函数" class="header-anchor">#</a> 加密函数</h1> <h2 id="checkkey"><a href="#checkkey" class="header-anchor">#</a> checkKey</h2> <p><strong>checkKey</strong> 用来检查一个已有密钥是否可用</p> <ul><li>类型定义</li></ul> <div class="language-ts extra-class"><pre class="language-ts"><code><span class="token function-variable function">checkKey</span><span class="token operator">:</span> <span class="token punctuation">(</span><span class="token parameter">yourKey<span class="token operator">:</span> <span class="token builtin">string</span></span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token builtin">boolean</span><span class="token punctuation">;</span>
</code></pre></div><ul><li>示例</li></ul> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">import</span> <span class="token punctuation">{</span> checkKey<span class="token punctuation">,</span> createKey <span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">&quot;siphan&quot;</span><span class="token punctuation">;</span>

<span class="token keyword">const</span> key <span class="token operator">=</span> <span class="token function">createKey</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token function">checkKey</span><span class="token punctuation">(</span>key<span class="token punctuation">)</span> <span class="token operator">===</span> <span class="token boolean">true</span><span class="token punctuation">;</span>
</code></pre></div><h2 id="createkey"><a href="#createkey" class="header-anchor">#</a> createKey</h2> <p><strong>createKey</strong> 用来生成一个密钥</p> <ul><li>类型定义</li></ul> <div class="language-ts extra-class"><pre class="language-ts"><code><span class="token function-variable function">createKey</span><span class="token operator">:</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token builtin">string</span>
</code></pre></div><ul><li>示例</li></ul> <div class="language-javascript extra-class"><pre class="language-javascript"><code><span class="token keyword">import</span> <span class="token punctuation">{</span> createKey <span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">&quot;siphan&quot;</span><span class="token punctuation">;</span>

<span class="token keyword">const</span> key <span class="token operator">=</span> <span class="token function">createKey</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><h2 id="encrypt"><a href="#encrypt" class="header-anchor">#</a> encrypt</h2> <p><strong>encrypt</strong> 用来加密非 UTF8 字符的密钥</p> <blockquote><p><strong>encrypt</strong> 函数直接对字符串进行加密，而不先进行编码</p></blockquote> <ul><li>类型定义</li></ul> <div class="language-ts extra-class"><pre class="language-ts"><code><span class="token keyword">function</span> <span class="token function">encrypt</span><span class="token punctuation">(</span><span class="token parameter">str<span class="token operator">:</span> <span class="token builtin">string</span></span><span class="token punctuation">)</span><span class="token operator">:</span> <span class="token punctuation">{</span>
    cipher<span class="token operator">:</span> <span class="token builtin">string</span><span class="token punctuation">;</span>
    solution<span class="token operator">:</span> <span class="token builtin">string</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre></div><blockquote><p><strong>cipher</strong> 是生成的密文，
<strong>solution</strong> 是生成的密钥</p></blockquote> <ul><li>示例</li></ul> <div class="language-javascript extra-class"><pre class="language-javascript"><code><span class="token keyword">import</span> <span class="token punctuation">{</span> encrypt <span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">&quot;siphan&quot;</span><span class="token punctuation">;</span>

<span class="token keyword">const</span> <span class="token punctuation">{</span> cipher<span class="token punctuation">,</span> solution <span class="token punctuation">}</span> <span class="token operator">=</span> <span class="token function">encrypt</span><span class="token punctuation">(</span><span class="token string">&quot;hello, world&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><h2 id="encryptutf8"><a href="#encryptutf8" class="header-anchor">#</a> encryptUTF8</h2> <p><strong>encryptUTF8</strong> 用来加密 UTF8 字符的密钥</p> <blockquote><p><strong>encrypt</strong> 函数先对字符串先进行编码，再进行加密</p></blockquote> <ul><li>类型定义</li></ul> <div class="language-ts extra-class"><pre class="language-ts"><code><span class="token keyword">function</span> <span class="token function">encryptUTF8</span><span class="token punctuation">(</span><span class="token parameter">str<span class="token operator">:</span> <span class="token builtin">string</span></span><span class="token punctuation">)</span><span class="token operator">:</span> <span class="token punctuation">{</span>
    cipher<span class="token operator">:</span> <span class="token builtin">string</span><span class="token punctuation">;</span>
    solution<span class="token operator">:</span> <span class="token builtin">string</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre></div><blockquote><p><strong>cipher</strong> 是生成的密文，
<strong>solution</strong> 是生成的密钥</p></blockquote> <ul><li>示例</li></ul> <div class="language-javascript extra-class"><pre class="language-javascript"><code><span class="token keyword">import</span> <span class="token punctuation">{</span> encryptUTF8 <span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">&quot;siphan&quot;</span><span class="token punctuation">;</span>

<span class="token keyword">const</span> <span class="token punctuation">{</span> cipher<span class="token punctuation">,</span> solution <span class="token punctuation">}</span> <span class="token operator">=</span> <span class="token function">encryptUTF8</span><span class="token punctuation">(</span><span class="token string">&quot;你好，世界&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><h2 id="encryptas"><a href="#encryptas" class="header-anchor">#</a> encryptAs</h2> <p><strong>encryptAs</strong> 用来加密非 UTF8 字符的密钥，并且生成给定的密钥</p> <blockquote><p><strong>encryptAs</strong> 函数不会先对密钥进行检查，如有必要，请先使用 <a href="/siphan/API/#createkey">checkkey</a> 检查</p></blockquote> <ul><li>类型定义</li></ul> <div class="language-ts extra-class"><pre class="language-ts"><code><span class="token keyword">function</span> <span class="token function">encryptAs</span><span class="token punctuation">(</span><span class="token parameter">str<span class="token operator">:</span> <span class="token builtin">string</span><span class="token punctuation">,</span> solution<span class="token operator">:</span> <span class="token builtin">string</span></span><span class="token punctuation">)</span><span class="token operator">:</span> <span class="token punctuation">{</span>
    cipher<span class="token operator">:</span> <span class="token builtin">string</span><span class="token punctuation">;</span>
    solution<span class="token operator">:</span> <span class="token builtin">string</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre></div><blockquote><p><strong>cipher</strong> 是生成的密文，
<strong>solution</strong> 是生成的密钥，并且一定和输入的函数参数 <strong>solution</strong> 相等</p></blockquote> <ul><li>示例</li></ul> <div class="language-javascript extra-class"><pre class="language-javascript"><code><span class="token keyword">import</span> <span class="token punctuation">{</span> encrypt <span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">&quot;siphan&quot;</span><span class="token punctuation">;</span>

<span class="token keyword">const</span> <span class="token punctuation">{</span> cipher<span class="token punctuation">,</span> solution <span class="token punctuation">}</span> <span class="token operator">=</span> <span class="token function">encrypt</span><span class="token punctuation">(</span><span class="token string">&quot;hello, world&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><h2 id="encryptutf8as"><a href="#encryptutf8as" class="header-anchor">#</a> encryptUTF8As</h2> <p><strong>encryptUTF8As</strong> 用来加密 UTF8 字符的密钥，并且生成给定的密钥</p> <blockquote><p>与 <strong>encryptUTF8As</strong> 函数相似， <strong>encryptUTF8As</strong> 函数也不会先对密钥进行检查</p></blockquote> <ul><li>类型定义</li></ul> <div class="language-ts extra-class"><pre class="language-ts"><code><span class="token keyword">function</span> <span class="token function">encryptUTF8As</span><span class="token punctuation">(</span><span class="token parameter">str<span class="token operator">:</span> <span class="token builtin">string</span><span class="token punctuation">,</span> solution<span class="token operator">:</span> <span class="token builtin">string</span></span><span class="token punctuation">)</span><span class="token operator">:</span> <span class="token punctuation">{</span>
    cipher<span class="token operator">:</span> <span class="token builtin">string</span><span class="token punctuation">;</span>
    solution<span class="token operator">:</span> <span class="token builtin">string</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre></div><blockquote><p><strong>cipher</strong> 是生成的密文，
<strong>solution</strong> 是生成的密钥，并且一定和输入的函数参数 <strong>solution</strong> 相等</p></blockquote> <ul><li>示例</li></ul> <div class="language-javascript extra-class"><pre class="language-javascript"><code><span class="token keyword">import</span> <span class="token punctuation">{</span> encryptUTF8As <span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">&quot;siphan&quot;</span><span class="token punctuation">;</span>

<span class="token keyword">const</span> <span class="token punctuation">{</span> cipher<span class="token punctuation">,</span> solution <span class="token punctuation">}</span> <span class="token operator">=</span> <span class="token function">encryptUTF8As</span><span class="token punctuation">(</span><span class="token string">&quot;你好，世界&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div></div> <footer class="page-edit"><!----> <!----></footer> <div class="page-nav"><p class="inner"><!----> <span class="next"><a href="/siphan/API/decrypt.html">
        解密函数
      </a>
      →
    </span></p></div> </main></div><div class="global-ui"></div></div>
    <script src="/siphan/assets/js/app.a096c374.js" defer></script><script src="/siphan/assets/js/2.b96ecc85.js" defer></script><script src="/siphan/assets/js/5.537dfae2.js" defer></script>
  </body>
</html>
